<template>
  <a-card :bordered="false" class="card-area">
    <div :class="advanced ? 'search' : null">
      <!-- 搜索区域 -->
      <a-form layout="horizontal">
        <div :class="advanced ? null: 'fold'">
          <a-row>
            <a-col :md="12" :sm="24">
              <a-form-item
                label="名称"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-input v-model="queryParams.maa01"/>
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24">
              <a-form-item
                label="创建时间"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <range-date @change="handleDateChange" ref="createTime"></range-date>
              </a-form-item>
            </a-col>
          </a-row>
        </div>

        <span style="float: right; margin-top: 3px;">
          <a-button type="primary" @click="search">查询</a-button>
          <a-button style="margin-left: 8px" @click="reset">重置</a-button>
        </span>
      </a-form>
    </div>

    <div>
      <div class="operator">
        <a-button v-hasPermission="'goods:add'" type="primary" ghost @click="add">新增</a-button>
        <a-button v-hasPermission="'goods:putAway'" @click="batchPutAway">上架</a-button>
        <a-button v-hasPermission="'goods:soldOut'" @click="batchSoldOut">下架</a-button>
        <a-button v-hasPermission="'goods:importExcel'" @click="importExcel">导入Excel</a-button>
        <a-dropdown v-hasPermission="'goods:export'">
          <a-menu slot="overlay">
            <a-menu-item key="export-data" @click="exportExcel">导出Excel</a-menu-item>
          </a-menu>
          <a-button>
            更多操作
            <a-icon type="down"/>
          </a-button>
        </a-dropdown>
        <a-button v-print="'#printTest'">打印表格</a-button>

      </div>
      <div id="printTest">
        <!-- 表格区域 -->
        <a-table :columns="columns"
                 :dataSource="dataSource"
                 :pagination="pagination"
                 :loading="loading"
                 :scroll="{ x: 900 }"
                 :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
                 @change="handleTableChange">
          <template slot="operation" slot-scope="text, record">
            <a-icon v-hasPermission="'goods:update'" type="setting" theme="twoTone" twoToneColor="#4a9ff5"
                    @click="edit(record)" title="修改"></a-icon>
            <a-badge v-hasNoPermission="'goods:update'" status="warning" text="无权限"></a-badge>
          </template>
        </a-table>
      </div>
    </div>
    <!-- 新增部门 -->
    <add-form
      @success="handleAddSuccess"
      @close="handleAddClose"
      :addVisiable="addVisiable">
    </add-form>

    <!-- 修改部门 -->
    <edit-form
      ref="editForm"
      @success="handleEditSuccess"
      @close="handleEditClose"
      :editVisiable="editVisiable">
    </edit-form>
    <import-excel-form
      ref="importExcelForm"
      @success="handleImportExcelSuccess"
      @close="handleImportExcelClose"
      :importExcelVisiable="importExcelVisiable">
    </import-excel-form>


  </a-card>
</template>

<script>
  import RangeDate from '@/components/datetime/RangeDate'
  import AddForm from './AddForm'
  import EditForm from './EditForm'
  import ImportExcelForm from './ImportExcelForm'

  export default {
    name: 'Goods',
    components: {AddForm, EditForm, ImportExcelForm, RangeDate},
    data() {
      return {
        advanced: false,
        dataSource: [],
        selectedRowKeys: [],
        queryParams: {},
        sortedInfo: null,
        pagination: {
          defaultPageSize: 10000000,
          hideOnSinglePage: true,
          indentSize: 100
        },
        loading: false,
        addVisiable: false,
        editVisiable: false,
        importExcelVisiable: false
      }
    },
    computed: {
      columns() {
        let {sortedInfo} = this
        sortedInfo = sortedInfo || {}
        return [{
          title: '商品SKU',
          dataIndex: 'maa01'
        }, {
          title: '供应商编码',
          dataIndex: 'maa02'
        }, {
          title: '创建时间',
          dataIndex: 'maacdate',
          sorter: true,
          sortOrder: sortedInfo.columnKey === 'maacdate'
        }, {
          title: '修改时间',
          dataIndex: 'maamdate',
          sorter: true,
          sortOrder: sortedInfo.columnKey === 'maacdate'
        }, {
          title: "商品状态",
          dataIndex: 'maaacti'
        }, {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: {customRender: 'operation'},
          fixed: 'right',
          width: 120
        }]
      }
    },
    mounted() {
      this.fetch()
    },
    methods: {
      onSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },
      handleAddClose() {
        this.addVisiable = false
      },
      handleAddSuccess() {
        this.addVisiable = false
        this.$message.success('新增商品成功')
        this.fetch()
      },
      add() {
        this.addVisiable = true
      },
      handleEditClose() {
        this.editVisiable = false
      },
      handleEditSuccess() {
        this.editVisiable = false
        this.$message.success('修改商品成功')
        this.fetch()
      },
      edit(record) {
        this.editVisiable = true
        this.$refs.editForm.setFormValues(record)
      },
      handleDateChange(value) {
        if (value) {
          this.queryParams.createTimeFrom = value[0]
          this.queryParams.createTimeTo = value[1]
        }
      },
      batchPutAway() {
        if (!this.selectedRowKeys.length) {
          this.$message.warning('请选择需要上架的记录')
          return
        }
        let that = this
        this.$confirm({
          title: '确定上架所选中的记录?',
          content: '当您点击确定按钮后，这些记录将会被上架出售！',
          centered: true,
          onOk() {
            let ids = []
            for (let key of that.selectedRowKeys) {
              ids.push(that.dataSource[key].maa01);
            }
            that.$put('goods/putAway/', {ids: ids.join(',')}).then(() => {
              that.$message.success('上架成功')
              that.selectedRowKeys = []
              that.fetch()
            })
          },
          onCancel() {
            that.selectedRowKeys = []
          }
        })
      },

      batchSoldOut() {
        if (!this.selectedRowKeys.length) {
          this.$message.warning('请选择需要下架的记录')
          return
        }
        let that = this
        this.$confirm({
          title: '确定下架所选中的记录?',
          content: '当您点击确定按钮后，这些记录将会被下架出售！',
          centered: true,
          onOk() {
            let ids = []
            for (let key of that.selectedRowKeys) {
              ids.push(that.dataSource[key].maa01);
            }
            that.$put('goods/soldOut/', {ids: ids.join(',')}).then(() => {
              that.$message.success('下架成功')
              that.selectedRowKeys = []
              that.fetch()
            })
          },
          onCancel() {
            that.selectedRowKeys = []
          }
        })
      },

      /*exportExcel() {
        let {sortedInfo} = this
        let sortField, sortOrder
        // 获取当前列的排序和列的过滤规则
        if (sortedInfo) {
          sortField = sortedInfo.field
          sortOrder = sortedInfo.order
        }
        this.$export('goods/excel', {
          sortField: sortField,
          sortOrder: sortOrder,
          ...this.queryParams
        })
      },*/    //  去掉功能
      search() {
        let {sortedInfo} = this
        let sortField, sortOrder
        // 获取当前列的排序和列的过滤规则
        if (sortedInfo) {
          sortField = sortedInfo.field
          sortOrder = sortedInfo.order
        }
        this.fetch({
          sortField: sortField,
          sortOrder: sortOrder,
          ...this.queryParams
        })
      },
      reset() {
        // 取消选中
        this.selectedRowKeys = []
        // 重置列排序规则
        this.sortedInfo = null
        // 重置查询参数
        this.queryParams = {}
        // 清空时间选择
        this.$refs.createTime.reset()
        this.fetch()
      },

      importExcel() {
        this.importExcelVisiable = true
      },
      handleImportExcelSuccess() {
        this.importExcelVisiable = false
        this.$message.success('导入商品成功')
        this.fetch()
      },
      handleImportExcelClose() {
        this.importExcelVisiable = false
      },
      handleTableChange(pagination, filters, sorter) {
        this.sortedInfo = sorter
        this.fetch({
          sortField: sorter.field,
          sortOrder: sorter.order,
          ...this.queryParams,
          ...filters
        })
      },
      fetch(params = {}) {
        this.loading = true
        this.$get('goods', {
          ...params
        }).then((r) => {
          let data = r.data
          this.loading = false
          this.dataSource = data.rows
        })
      },

      start_print() {

      }

    }
  }
</script>

<style lang="less" scoped>
  @import "../../../../static/less/Common";
</style>
